package com.vitogiovanni.tobook.utilities;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.vitogiovanni.tobook.R;

public class ElementiMenuListViewAdapter extends ArrayAdapter<ElementoMenuItem>{
	
	Context context;
	private static final int TYPE_TAB = 0;
    private static final int TYPE_ELE = 1;
	
	public ElementiMenuListViewAdapter(Context context, int resource,
			List<ElementoMenuItem> objects) {
		super(context, resource, objects);
		this.context = context;
	}
	
	private class ViewHolder {
		TextView nomeElemento;
    }
	
	@Override
	public int getViewTypeCount() {
	    return 2;
	}

	@Override
	public int getItemViewType(int position) {
	    return getItem(position).isSeparator()? TYPE_TAB : TYPE_ELE;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        ElementoMenuItem elementoMenuItem = getItem(position);
        
        int type = getItemViewType(position);
        
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
        	convertView = mInflater.inflate(R.layout.list_item_elemento_menu, null);
            holder = new ViewHolder();
            holder.nomeElemento = (TextView) convertView.findViewById(R.id.lista_elementi_menu);

            switch(type){
            case TYPE_TAB:
				holder.nomeElemento.setOnClickListener(null);
				holder.nomeElemento.setOnLongClickListener(null);
				holder.nomeElemento.setLongClickable(false);
				holder.nomeElemento.setFocusable(false);
				holder.nomeElemento.setTextColor(Color.BLACK);
				holder.nomeElemento.setPadding(0, 10, 0, 10);
				holder.nomeElemento.setBackgroundColor(Color.TRANSPARENT);
				break;
            case TYPE_ELE:
            	holder.nomeElemento.setPadding(0, 5, 0, 5);
				holder.nomeElemento.setTextColor(Color.GRAY);
				holder.nomeElemento.setBackgroundColor(Color.TRANSPARENT);
				break;
            }
            
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
 
        holder.nomeElemento.setText(elementoMenuItem.getNome());

        return convertView;
    }

}
